package com.meichis.yndms.adapter;

import android.view.View;

import com.meichis.mcsappframework.adapter.slidelistview.ViewHolder;
import com.meichis.mcsappframework.adapter.slidelistview.base.ItemViewDelegate;
import com.meichis.mcsappframework.pulltorefresh.view.SlideView;
import com.meichis.mcsappframework.utils.UniversalInterface;
import com.meichis.mcsappframework.widget.ZoomImageView;
import com.meichis.yndms.R;
import com.meichis.yndms.model.UploadProgress;
import com.meichis.yndms.room.AppDatabase;
import com.nostra13.universalimageloader.core.DisplayImageOptions;

import java.util.List;

public class UploadProgress_1 implements ItemViewDelegate<UploadProgress> {

    private List<UploadProgress> datas;
    private UniversalInterface<Void, Void> notifyData;
    private DisplayImageOptions options;

    public UploadProgress_1(List<UploadProgress> datas, UniversalInterface<Void, Void> notifyData, DisplayImageOptions options) {
        this.datas = datas;
        this.notifyData = notifyData;
        this.options = options;
    }

    @Override
    public int getItemViewLayoutId() {
        return R.layout.activity_uploadprogress_item;
    }

    @Override
    public int getSlideViewLayoutId() {
        return R.layout.slideview_delete;
    }

    @Override
    public boolean isForViewType(UploadProgress item, int position) {
        return !item.isDelivery();
    }

    @Override
    public void convert(ViewHolder viewHolder, final UploadProgress item, int position) {
        ZoomImageView ivPic = viewHolder.getView(R.id.iv_pic);
        if (item.getFilepath().length() > 0) {
            ivPic.setVisibility(View.VISIBLE);
            ivPic.displayImage("file:///" + item.getFilepath(), options);
        } else {
            ivPic.setVisibility(View.GONE);
        }
        viewHolder.setText(R.id.tv_text, item.getText());
        viewHolder.setText(R.id.tv_uperror, item.getUperror());
        SlideView slideView = (SlideView) viewHolder.getConvertView();
        slideView.setIsCanSlide(true);
        viewHolder.setOnClickListener(R.id.tv_delete, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean success = false;
                if (item.getFilepath().length() > 0) {
                    success = AppDatabase.getInstance().pictureDao().delete(item.getGUID()) > 0;
                } 
                if (success) {
                    datas.remove(item);
                    notifyData.todo(null);
                }
            }
        });
    }
}
